﻿Imports System
Imports System.Collections.Generic
Imports System.Diagnostics
Imports System.Globalization
Imports System.Workflow.Activities
Imports System.Workflow.ComponentModel

Namespace System.Workflow.Activities.Rules
    Friend Class Tracer
        ' Methods
        Friend Sub New(ByVal name As String, ByVal activityExecutionContext As ActivityExecutionContext)
            If (Not activityExecutionContext Is Nothing) Then
                Me.tracePrefix = String.Format(CultureInfo.CurrentCulture, Tracer.traceRuleIdentifier, New Object() { name, activityExecutionContext.ContextGuid.ToString })
            Else
                Me.tracePrefix = String.Format(CultureInfo.CurrentCulture, Tracer.traceRuleHeader, New Object() { name })
            End If
        End Sub

        Friend Sub RuleResult(ByVal ruleName As String, ByVal result As Boolean)
            WorkflowActivityTrace.Rules.TraceEvent(TraceEventType.Information, 0, Tracer.traceRuleResult, New Object() { Me.tracePrefix, ruleName, result.ToString })
        End Sub

        Friend Sub StartActions(ByVal ruleName As String, ByVal result As Boolean)
            WorkflowActivityTrace.Rules.TraceEvent(TraceEventType.Verbose, 0, Tracer.traceRuleActions, New Object() { Me.tracePrefix, If(result, Tracer.traceThen, Tracer.traceElse), ruleName })
        End Sub

        Friend Sub StartRule(ByVal ruleName As String)
            WorkflowActivityTrace.Rules.TraceEvent(TraceEventType.Verbose, 0, Tracer.traceRuleEvaluate, New Object() { Me.tracePrefix, ruleName })
        End Sub

        Friend Sub StartRuleSet()
            WorkflowActivityTrace.Rules.TraceEvent(TraceEventType.Information, 0, Tracer.traceRuleSetEvaluate, New Object() { Me.tracePrefix })
        End Sub

        Friend Sub TraceConditionSymbols(ByVal ruleName As String, ByVal symbols As ICollection([Of] String))
            Me.TraceRuleSymbols(Tracer.traceRuleConditionDependency, Tracer.traceCondition, ruleName, symbols)
        End Sub

        Friend Sub TraceElseSymbols(ByVal ruleName As String, ByVal symbols As ICollection([Of] String))
            Me.TraceRuleSymbols(Tracer.traceRuleActionSideEffect, Tracer.traceElse, ruleName, symbols)
        End Sub

        Friend Sub TraceElseTriggers(ByVal currentRuleName As String, ByVal triggeredRules As ICollection([Of] Integer), ByVal ruleStates As List([Of] RuleState))
            Me.TraceRuleTriggers(Tracer.traceElse, currentRuleName, triggeredRules, ruleStates)
        End Sub

        Private Sub TraceRuleSymbols(ByVal message As String, ByVal clause As String, ByVal ruleName As String, ByVal symbols As ICollection([Of] String))
            Dim str As String
            For Each str In symbols
                WorkflowActivityTrace.Rules.TraceEvent(TraceEventType.Verbose, 0, message, New Object() { Me.tracePrefix, ruleName, clause, str })
            Next
        End Sub

        Private Sub TraceRuleTriggers(ByVal thenOrElse As String, ByVal currentRuleName As String, ByVal triggeredRules As ICollection([Of] Integer), ByVal ruleStates As List([Of] RuleState))
            Dim num As Integer
            For Each num In triggeredRules
                WorkflowActivityTrace.Rules.TraceEvent(TraceEventType.Verbose, 0, Tracer.traceRuleTriggers, New Object() { Me.tracePrefix, currentRuleName, thenOrElse, ruleStates.Item(num).Rule.Name })
            Next
        End Sub

        Friend Sub TraceThenSymbols(ByVal ruleName As String, ByVal symbols As ICollection([Of] String))
            Me.TraceRuleSymbols(Tracer.traceRuleActionSideEffect, Tracer.traceThen, ruleName, symbols)
        End Sub

        Friend Sub TraceThenTriggers(ByVal currentRuleName As String, ByVal triggeredRules As ICollection([Of] Integer), ByVal ruleStates As List([Of] RuleState))
            Me.TraceRuleTriggers(Tracer.traceThen, currentRuleName, triggeredRules, ruleStates)
        End Sub

        Friend Sub TraceUpdate(ByVal ruleName As String, ByVal otherName As String)
            WorkflowActivityTrace.Rules.TraceEvent(TraceEventType.Verbose, 0, Tracer.traceUpdate, New Object() { Me.tracePrefix, ruleName, otherName })
        End Sub


        ' Fields
        Private Shared traceCondition As String = Messages.Condition
        Private Shared traceElse As String = Messages.Else
        Private tracePrefix As String
        Private Shared traceRuleActions As String = Messages.TraceRuleActions
        Private Shared traceRuleActionSideEffect As String = Messages.TraceRuleActionSideEffect
        Private Shared traceRuleConditionDependency As String = Messages.TraceRuleConditionDependency
        Private Shared traceRuleEvaluate As String = Messages.TraceRuleEvaluate
        Private Shared traceRuleHeader As String = Messages.TraceRuleHeader
        Private Shared traceRuleIdentifier As String = Messages.TraceRuleIdentifier
        Private Shared traceRuleResult As String = Messages.TraceRuleResult
        Private Shared traceRuleSetEvaluate As String = Messages.TraceRuleSetEvaluate
        Private Shared traceRuleTriggers As String = Messages.TraceRuleTriggers
        Private Shared traceThen As String = Messages.Then
        Private Shared traceUpdate As String = Messages.TraceUpdate
    End Class
End Namespace

